package com.xmut.bs.handaccount.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.base.ToolbarBaseActivity;
import com.xmut.bs.handaccount.bmob.User;
import com.xmut.bs.handaccount.util.DateUtils;
import com.xmut.bs.handaccount.util.StringUtil;
import com.xmut.bs.handaccount.util.TimeSelectUtil;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobDate;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;


public class EditPersonInfoActivity extends ToolbarBaseActivity {

    @BindView(R.id.profiles_et)
    MaterialEditText mProfilesEt;
    @BindView(R.id.edit_cv)
    CardView mEditCv;
    @BindView(R.id.textView)
    TextView mTextView;
    @BindView(R.id.right_tv)
    TextView mRightTv;
    @BindView(R.id.radio_gp)
    RadioGroup mRadioGp;
    @BindView(R.id.radio_cv)
    CardView mRadioCv;
    @BindView(R.id.mask_view)
    View mMaskView;
    @BindView(R.id.none_btn)
    RadioButton mDevBtn;
    @BindView(R.id.male_btn)
    RadioButton mOperateBtn;
    @BindView(R.id.female_btn)
    RadioButton mIssueBtn;

    public static final String PARAM_SEX = "sex";
    public static final String PARAM_NICK = "nickname";
    public static final String PARAM_BIRTHDAY = "birthday";
    public static final String PARAM_SIGNATURE = "signature";
    public static final String PARAM_TYPE = "type";
    public static final String PARAM_VALUE = "value";

    private int sex;

    private String type;
    private BottomSheetDialog dialog;

    @Override
    protected String getSubTitle() {
        switch (type) {
            default:
                return "修改信息";
        }
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_edit_person_info;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mRightTv.setText("保存");
        switch (type) {
            case PARAM_BIRTHDAY:
                mProfilesEt.setHint("请选择生日日期");
                mProfilesEt.setFocusable(false);
                mProfilesEt.setFocusableInTouchMode(false);
                mMaskView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (dialog == null)
                            dialog = TimeSelectUtil.createDialog(mContext, TimeSelectUtil.TYPE_DATE, false, new TimeSelectUtil.DateSelectListener() {
                                @Override
                                public void onDateSelectListener(DatePicker datePicker, TimePicker timePicker) {
                                    mProfilesEt.setText(TimeSelectUtil.getDate(datePicker));
                                    dialog.dismiss();
                                }
                            });
                        dialog.show();
                    }
                });
                mMaskView.setVisibility(View.VISIBLE);
                break;
            case PARAM_NICK:
                mProfilesEt.setHint("请输入名称");
                break;
            case PARAM_SEX:
                mEditCv.setVisibility(View.GONE);
                mRadioCv.setVisibility(View.VISIBLE);
                sex = 0;
                break;
            case PARAM_SIGNATURE:
                mProfilesEt.setHint("请输入个人签名");
                break;
            default:
                mProfilesEt.setHint("请输入修改内容");
                break;
        }
        mRadioGp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.none_btn:
                        sex = 0;
                        break;
                    case R.id.male_btn:
                        sex = 1;
                        break;
                    case R.id.female_btn:
                        sex = 2;
                        break;
                }
            }
        });
    }

    @Override
    protected void initParams(Intent intent) {
        type = intent.getStringExtra(PARAM_TYPE);
    }

    @OnClick({R.id.right_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.right_tv:
                User user = BmobUser.getCurrentUser(User.class);
                if (type.equals(PARAM_SEX)) {
                    user.setSex(sex);
                } else {
                    if (StringUtil.isNull(mProfilesEt.getText().toString())) {
                        showToast("修改内容不能为空");
                        return;
                    }
                    if (type.equals(PARAM_BIRTHDAY)) {
                        BmobDate data = new BmobDate(DateUtils.StringToDateWithSprit(mProfilesEt.getText().toString().trim()));
                        user.setBirthday(data);
                    } else if(type.equals(PARAM_NICK)){
                        user.setNickName(mProfilesEt.getText().toString());
                    }
                }
                user.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            showToast("修改成功");
                            Intent intent = new Intent();
                            String value = mProfilesEt.getText().toString().trim();
                            switch (type) {
                                case PARAM_SEX:
                                    value = String.valueOf(sex);
                                    break;
                            }
                            intent.putExtra(PARAM_VALUE, value);
                            //            intent.putExtra(PARAM_TYPE, type);
                            setResult(RESULT_OK, intent);
                            finish();
                        } else {
                            showToast("修改失败：" + e.getMessage());
                        }
                    }
                });
                break;
        }
    }
}
